﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace XsdSort
{
    class Program
    {
        static void Main(string[] args)
        {
            //var inputPath = @"D:\work\個人フォルダ\crf\CW1\schema\H43TR2\Native XML (GP1 v25.4.7.172)\NativeOrganization.xsd";
            //var outputPath = @"D:\work\個人フォルダ\crf\CW1\schema\H43TR2\Native XML (GP1 v25.4.7.172)\NativeOrganization_sort.xsd";
            ////new XsdReorderProcessor().ProcessXsdFile(inputPath, outputPath);
            ////new XsdSorter().SortXsdElements(inputPath, outputPath);
            //new XsdRecursiveSorter().SortXsdRecursive(inputPath, outputPath);
           
            try
            {
                RunWithConfig();

                // 添加结束暂停 ▼
                Console.WriteLine("\n处理完成，按任意键退出...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                // 异常处理 ▼
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"错误发生：{ex.Message}");
                Console.ResetColor();
                Console.WriteLine("按任意键退出...");
                Console.ReadKey();
                Environment.ExitCode = 1;
            }
        }
        // 新增配置类
        public class AppConfig
        {
            public ProcessingPathsConfig ProcessingPaths { get; set; }
        }

        public class ProcessingPathsConfig
        {
            public string InputDir { get; set; }
            public string OutputDir { get; set; }
        }

        // 修改后的入口方法
        public static void RunWithConfig(string configPath = "InfoSetting.json")
        {
            if (!File.Exists(configPath))
                throw new FileNotFoundException($"Config file {configPath} not found");

            var config = JsonSerializer.Deserialize<AppConfig>(
                File.ReadAllText(configPath),
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
            );
            //// 新增路径格式转换
            //config.ProcessingPaths.InputDir = config.ProcessingPaths.InputDir?.Replace('\\', '/');
            //config.ProcessingPaths.OutputDir = config.ProcessingPaths.OutputDir?.Replace('\\', '/');
            if (!Directory.Exists(config.ProcessingPaths.InputDir))
                throw new DirectoryNotFoundException($"Input directory not found");

            if (!Directory.Exists(config.ProcessingPaths.OutputDir))
                throw new DirectoryNotFoundException($"Output directory not found");

            new XsdRecursiveSorter().ProcessDirectory(config.ProcessingPaths.InputDir,
                            config.ProcessingPaths.OutputDir);
        }
    }
}
